/*本模块功能:成绩查询
        编写者:朱光曜
        编写时间:2017.10.31*/
$(function() {
	
	var string1 = '<tr>' +
		'<th class="col-lg-2">编号</th>' +
		'<th class="col-lg-7">课程</th>' +
		'<th class="col-lg-4">平时成绩</th>' +
		'<th class="col-lg-5">总成绩</th>' +
		'<th class="col-lg-3">操作</th>' +
		'</tr>';

	function tabelModel(data) {
		var str="";
		if(data.memberList[key].classEndMark==0){
			str="repairBtn";
		}else{
			str="";
		}
		
		
		return '<tr data-num="0" classId="'+data.memberList[key].classId+'"   courseId="'+data.memberList[key].courseId+'"  stuorteaId="'+data.memberList[key].stuorteaId+'">' +
			'<td class="col-lg-2">'+(parseInt(key)+1) +'</td>' +
			'<td class="col-lg-7">'+data.memberList[key].courseName+'</td>' +
			'<td class="col-lg-4">'+data.memberList[key].preforSource+'分</td>' +
			'<td class="col-lg-5">'+data.memberList[key].sumSource+'分</td>' +
			'<td class="col-lg-3 last">' +
			'<a href="javascript:;" class="'+str+'">详情</a>' +
			'</td>' +
			'</tr>';

	}
	var loading1 = new Loading();//监控全局ajax添加loading
	loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
	
		});
	$(document).ajaxStop(function(){
		loading1.hide();
		
		});
	var nowData = 1;
	var index = {

		init: function() {
			this.bind();
		},
		bind: function() {
			var self = this;
			this.data_content = {}; //搜索集
			this.tab_content = {}; //列表集
			this.parameter = {
				"pageNow": 1,
				"pageSize": 10
			};

			this.postData(this.parameter, nowData);
            $(document).off("click", ".tableBox td.last .repairBtn").on("click", ".tableBox td.last .repairBtn", function() { //详情点击
				
				
				var json=JSON.stringify({
					"pageName":"studentScoreQueryMessage",
					"paramData":{
						"classId":parseInt($(this).parents("tr").attr("classId")),
				        "courseId":parseInt($(this).parents("tr").attr("courseId")),
				        "stuorteaId":$(this).parents("tr").attr("stuorteaId")
					}
				});
				window.location.href="skipPage?jsonStr="+json;
			});
			

			
		},
		add: function() { //搜索追加
			var self = this;
			var data = self.data_content;
			
			for(key in data.courseList) {
			
				$("#courseNames").append("<option  courseId='"+data.courseList[key].courseId+"'>" + data.courseList[key].courseName + "</option>");
			}
			
			$(".funBox .searchBtn").off("click").click(function() { //搜索点击
				
				
				var courseId = $("#courseNames option:selected").attr("courseId");
				var nowData = 1;
				var parameter = {
					
					"courseId": courseId,
					"pageNow": 1,
					"pageSize": 10
				};
				//遍历删除空元素
				for(i in parameter){
					
					if(parameter[i]==null||parameter[i]==""){
						delete parameter[i];
					}
					if(!isNaN(parameter[i])){
						parameter[i]=Number(parameter[i]);
					}
				}

				
				self.postData(parameter, nowData,1);
			});

		},
		tab_add: function() { //列表追加
			var self = this;
			var data = self.tab_content;
			var table = string1;
			//console.log(data.memberList)
			//data.memberList={};
			var i = 0;
			for(key in data.memberList) {
				i++;
			}
			
			if(i > 0) {

				for(key in data.memberList) {

					table += tabelModel(data);
					//console.log(table)
				}

				$(".mainContent .tableBox").html(table);
			} else {
				var trImg = "<tr><td colspan='5'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
				$(".mainContent .tableBox").html(string1+ trImg);
			}
			//设置列表宽度(最大为9)
			var numMax = 0;
			$(".tableBox tr th").each(function() {
				var str = $(this).attr("class");
				var num = str.lastIndexOf("col-lg-") + 7;
				var num1 = parseFloat(str.substring(num, num + 1));
				numMax += num1;
			});
			
			$(".tableBox tr th").each(function() {
				var str = $(this).attr("class");
				var num = str.lastIndexOf("col-lg-") + 7;
				var num1 = parseFloat(str.substring(num, num + 1));
				//console.log(num1 / numMax)
				$(this).css("width", num1 / numMax * 100 + "%");
			});
		},
		postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
			var self = this;
			var parameter = par_data;

			$.ajax({
				type: "post",
				url: "getStuAllSourceList",
				async: true,
				data:{
					"jsonStr":	JSON.stringify(parameter)
				},
				success: function(data, status, xhr) {
					var new_data = JSON.parse(data);

                    if(new_data.result == "success"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }
                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = 1;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };
                                    //console.log(parameter)
                                } else {
                                    parameter.pageNow = nowpage;
                                    //console.log(parameter)
                                }
                                $.ajax({
                                    type: "post",
                                    url: "getStuAllSourceList",
                                    async: true,
                                    data:{
                                        "jsonStr":	JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取院系
                                        var msgNew= JSON.parse(msg);
                                        //console.log(msgNew)
                                        self.tab_content = msgNew;

                                        self.tab_add();
                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });
					}else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
					}

				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;
				}
			});
		}

	};

	index.init();

});